<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\IndexManage;
use common\models\GoodsDetail;

/**
 * IndexManageSearch represents the model behind the search form about `common\models\IndexManage`.
 */
class IndexManageSearch extends IndexManage
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'goods_id', 'type', 'sort', 'status'], 'integer'],
            [['recommend_starttime', 'recommend_endtime', 'add_time', 'create_user'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = IndexManage::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'goods_id' => $this->goods_id,
            'type' => $this->type,
            'sort' => $this->sort,
            'status' => $this->status,
        ]);
        if(empty($this->recommend_endtime))
        {
            $query->andFilterWhere(['>=', 'recommend_starttime', $this->recommend_starttime]);
        }elseif(empty($this->recommend_starttime))
        {
            $query->andFilterWhere(['<=', 'recommend_starttime',  $this->recommend_endtime]);
        }else
        {
            $query->andFilterWhere(['between', 'recommend_starttime', $this->recommend_starttime, $this->recommend_endtime]);
        }
        

        return $dataProvider;
    }
    
    public static function getGoodsInfos()
    {
       return  GoodsDetail::find()->all();
    }
}
